<template>
    <view class="container">
        <view class="logo">
            <image src="/static/logo.png" mode="aspectFit"></image>
        </view>
        
        <view class="title">
            <text>Mood Box</text>
            <text class="subtitle">记录心情，了解自己</text>
        </view>

        <view class="login-section">
            <button 
                class="wx-login-btn" 
                open-type="getUserInfo" 
                @getuserinfo="onGetUserInfo"
            >
                <text class="iconfont icon-wx">&#xe603;</text>
                微信登录
            </button>
        </view>

        <view class="privacy-tips">
            <text>登录即表示同意</text>
            <text class="privacy-link" @tap="showPrivacy">《隐私政策》</text>
        </view>
    </view>
</template>

<script>
import authApi from '@/api/auth';

export default {
    data() {
        return {
            isLoading: false
        }
    },
    onLoad() {
        // 检查是否已登录
        if (authApi.checkLoginStatus()) {
            this.navigateToHome();
        }
    },
    methods: {
        async onGetUserInfo(e) {
            if (this.isLoading) return;
            this.isLoading = true;

            try {
                // 调用微信登录获取 code
                const [loginErr, loginRes] = await uni.login();
                if (loginErr) {
                    throw new Error('微信登录失败');
                }

                // 使用 code 进行后端登录
                const loginResult = await authApi.wxLogin(loginRes.code);

                // 保存 token 和 userId
                uni.setStorageSync('token', loginResult.token);
                uni.setStorageSync('userId', loginResult.userId);

                // 更新用户信息
                await authApi.updateUserInfo({
                    nickName: e.detail.userInfo.nickName,
                    avatarUrl: e.detail.userInfo.avatarUrl
                });

                // 跳转到首页
                this.navigateToHome();
            } catch (error) {
                uni.showToast({
                    title: error.message || '登录失败',
                    icon: 'none'
                });
            } finally {
                this.isLoading = false;
            }
        },
        navigateToHome() {
            uni.switchTab({
                url: '/pages/index/index'
            });
        },
        showPrivacy() {
            uni.navigateTo({
                url: '/pages/privacy/privacy'
            });
        }
    }
}
</script>

<style scoped>
.container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100vh;
    background-color: #F8F8F8;
    padding: 0 40rpx;
}

.logo {
    width: 200rpx;
    height: 200rpx;
    margin-bottom: 40rpx;
}

.logo image {
    width: 100%;
    height: 100%;
}

.title {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 60rpx;
}

.title text:first-child {
    font-size: 48rpx;
    font-weight: bold;
    color: #333;
}

.subtitle {
    font-size: 28rpx;
    color: #666;
    margin-top: 10rpx;
}

.login-section {
    width: 100%;
}

.wx-login-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #3cc51f;
    color: white;
    border: none;
    padding: 20rpx;
    border-radius: 10rpx;
    font-size: 32rpx;
}

.wx-login-btn .iconfont {
    margin-right: 20rpx;
    font-size: 40rpx;
}

.privacy-tips {
    margin-top: 40rpx;
    font-size: 24rpx;
    color: #999;
    display: flex;
    align-items: center;
}

.privacy-link {
    color: #3cc51f;
    margin-left: 10rpx;
}
</style> 